using Digitalmes.Domain.Entities.Mes.Productions;

namespace Digitalmes.Application.Mes.Traceability.Products.Queries;

/// <summary>
/// 产品生产工艺步骤查询命令。
/// </summary>
/// <param name="Sn">零件码</param>
/// <param name="LineId">产线 Id</param>
public sealed record GetProcessStepQuery(string Sn, long LineId) : IRequest<IResult<List<ProdStepExecutionDto>>>;

internal sealed class GetProcessStepQueryHandler(IRepository<ProdStepExecution> stepExecutionRepo) : IRequestHandler<GetProcessStepQuery, IResult<List<ProdStepExecutionDto>>>
{
    public async Task<IResult<List<ProdStepExecutionDto>>> Handle(GetProcessStepQuery request, CancellationToken cancellationToken)
    {
        // 查找执行记录（可重复过站）
        var stepExecutions = await stepExecutionRepo.GetListAsync(m => m.Sn == request.Sn && m.LineId == request.LineId, cancellationToken);
        return Result.From(stepExecutions.Adapt<List<ProdStepExecutionDto>>());
    }
}
